package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {
    @Select("select * from user_account where user_id = #{userId} and available_amount >= #{amount} for update")
    UserAccount check(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    @Update("""
            update user_account
            		set lock_amount = lock_amount + #{amount}, available_amount = available_amount - #{amount}
                     where user_id = #{userId}
            """)
    int lock(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    @Update("""
            update user_account
               set lock_amount = lock_amount - #{amount}, total_amount = total_amount - #{amount}, total_pay_amount = total_pay_amount + #{amount}
               where user_id = #{userId}
            """)
    int minus(@Param("userId") Long userId, @Param("amount") BigDecimal amount);

    int unLock(Long userId, BigDecimal amount);
}
